import Element from "element-ui"

let wsUrl = 'ws://localhost:8808/'
let server = '';
let isConnected = false;

function initWebsocket(api) {
    const url = wsUrl + api
    server = new WebSocket(url);
    server.onmessage = wsOnMessage;
    server.onopen = wsOnOpen;
    server.onerror = wsOnError;
    server.onclose = wsOnClose;
}

function wsOnOpen(){
    isConnected = true;
    console.log("开启websocket")
}
function wsOnError(e) {
    console.error(e);
}
function wsOnMessage(e) {
    const respData = JSON.parse(e.data);
    const self = this;
    console.log(respData.value)

}
function wsSend(Data) {
    let jsonData = JSON.stringify(Data)
    server.send(Data);
}
function wsOnClose(e){
    console.log('server close!',e);
}



export {initWebsocket,wsSend,wsOnClose}